package com.sen.paydemo.controller;

import com.sen.paydemo.entity.Product;
import com.sen.paydemo.param.ProductParam;
import com.sen.paydemo.service.ProductService;
import com.sen.paydemo.vo.ApiResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@CrossOrigin // 开发前端的跨域访问
@Tag(name = "商品管理")
@RestController
@RequestMapping("/api/product")
@Slf4j
public class ProductController extends BaseController {

    @Resource
    private ProductService productService;


    @Operation(summary = "测试接口", description = "接口详细信息,可以不用")
    @GetMapping("/test")
    public ApiResult test() {

        return success("测试成功", new Date());
    }

    @Operation(summary = "商品列表")
    @GetMapping("/list")
    public ApiResult<List<Product>> list() {

        return success(productService.list());

    }

    @Operation(summary = "查询全部商品列表")
    @GetMapping()
    public ApiResult<List<Product>> list(ProductParam param) {

        return success(productService.list());

    }

    @Operation(summary = "根据id查询商品列表")
    @GetMapping("/{id}")
    public ApiResult<Product> get(@PathVariable("id") Integer id) {
        return success(productService.getById(id));
    }


    @Operation(summary = "添加商品列表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Product product) {
        if (productService.save(product)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "修改商品列表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Product product) {
        if (productService.updateById(product)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }


    @Operation(summary = "删除商品列表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (productService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @Operation(summary = "批量删除商品列表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (productService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }
}
